using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace GMailNotifierNET.Classes.Animations
{
    class AnimationRunner
    {
        private List<AnimationBase> m_arrAnimations = new List<AnimationBase>();

        private List<AnimationBase> m_arrCachedAnimations = new List<AnimationBase>();
        private List<AnimationBase> m_arrFinishedAnimations = new List<AnimationBase>();

        public event Animations.AnimationFinishedDelegate AnimationFinished;

        public AnimationRunner()
        {         
        }

        public List<AnimationBase> Animations
        {
            get { return this.m_arrAnimations; }
        }

        public void StartAnimations(Form form)
        {
            foreach (AnimationBase animation in this.m_arrAnimations)
                this.m_arrCachedAnimations.Add(animation);
            
            for (int i = 0; i < this.m_arrCachedAnimations.Count; i++)
            {
                AnimationBase animation = this.m_arrCachedAnimations[i];
                animation.AnimationFinished += new AnimationFinishedDelegate(animation_AnimationFinished);

                animation.StartAnimation(form);
            }
        }

        void animation_AnimationFinished(object sender, EventArgs e)
        {
            AnimationBase finishedAnimation = (AnimationBase)sender;

            this.m_arrFinishedAnimations.Add(finishedAnimation);
            this.m_arrCachedAnimations.Remove(finishedAnimation);

            if (this.m_arrCachedAnimations.Count == 0)
            {
                if (AnimationFinished != null)
                    AnimationFinished(this, EventArgs.Empty);
            }
        }
    }
}
